<template>
    <rx-dialog @handOk="handSave" @cancel="cancel">
        <template #toolbar>
            <a-checkbox :checked="checkStrictly" @change="changeCheckStrictly(checkStrictly)">
                {{$ti18('cascadeSelect','级联选择','osGroupPrivilege')}}
            </a-checkbox>
            <a-checkbox :checked="checkAll" @change="onChange">
                {{$ti18('rxList.selectAll','全选')}}
            </a-checkbox>
        </template>
        <a-form :form="form">
            <a-form-item style="display:none">
                <a-input v-decorator="['groupId']"/>
            </a-form-item>
            <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label=""
            >
                <rx-vue-tree
                    :tree-data="permissions"
                    v-model="checkedKeys"
                    :label="'title'"
                    :field="'key'"
                    :show-checkbox="true"
                    :check-parents="true"
                    :check-strictly="checkStrictly"
                    :showIcon="true"
                >
                </rx-vue-tree>
            </a-form-item>
        </a-form>
    </rx-dialog>
</template>

<script>
import SysMenuApi from '@/api/system/core/sysMenu'
import OsGroupMenuApi from '@/api/user/org/osGroupMenu'
import OsGroupApi from '@/api/user/org/osGroup';
import {BaseForm, Util,RxVueTree} from "@lowcode/jpaas-common-lib"

export default {
    name: 'OsGroupMenuEdit',
    mixins: [BaseForm],
    props: ['tenantId', 'appId','dimId'],
    components:{RxVueTree},
    data() {
        return {
            permissions: [],
            treeCheck: false,
            groupId: null,
            allMenusList: [],
            checkedKeys: [],
            checkAll: false,
            checkStrictly: true,
            //暂存父节点Id
            parentIds: [],
        }

    },
    created() {
        this.loadPermissions();
    },
    methods: {
        get(id) {
            return new Promise((resolve,reject)=>{resolve({})});
        },
        loadGroupMenu() {
            var self_ = this;
            OsGroupMenuApi.findMenuListByGroupId({groupId: this.pkId}).then(res => {
                let data = res.data;
                let arr = [];
                for (let i = 0; i < data.length; i++) {
                    arr.push(data[i])
                }
                self_.checkedKeys = Array.from(new Set(arr));
                if (data.length == self_.allMenusList.length) {
                    self_.checkAll = true;
                }
            })
        },
        loadPermissions() {
            var self_ = this;
            var parameter = {
                type: 'group'
            }
            if (this.tenantId) {
                parameter.tenantId = this.tenantId;
            }
            if (this.appId) {
                parameter.appId = this.appId;
            }
            SysMenuApi.selectMenus(parameter).then(res => {
                self_.allMenusList = res;
                self_.buildtree(res, self_.permissions, '0');
                self_.loadGroupMenu();
            })
        },
        buildtree(list, permissions, parentId) {
            let self_ = this;
            list.forEach(item => {
                if (item.parentId === parentId) {
                    var child = {
                        key: item.id,
                        title: item.name,
                        parentId: item.parentId,
                        slots: {icon: item.menuType},
                        children: []
                    }
                    this.buildtree(list, child.children, item.id);
                    permissions.push(child)
                }
            })
        },

        onChange(e) {
            this.checkAll = e.target.checked;
            const {allMenusList} = this;
            this.checkedKeys = [];
            if (this.checkAll) {
                for (var i = 0; i < allMenusList.length; i++) {
                    this.checkedKeys.push(allMenusList[i].id);
                }
            }
        },
        handSave() {
            let data = [];
            let self_ = this;
            this.checkedKeys.push(...this.parentIds);
            //去重
            this.checkedKeys=Array.from(new Set(this.checkedKeys));
            let len = 0 ;//记录长度，优化循环
            for (let item of this.allMenusList){
                if(len == this.checkedKeys.length){
                    break ;
                }
                if (this.checkedKeys.includes(item.id)) {
                    let obj = {
                        menuId: item.id,
                        appId: item.appId,
                        name: item.name,
                        parentId: item.parentId
                    }
                    data.push(obj);
                    len += 1;
                }
            }
            OsGroupMenuApi.saveOsGroupMenu(data, self_.pkId,false,self_.dimId).then(res => {
                if (res.success) {
                    Util.closeWindow(self_, "ok");
                }
            });
        },
        changeCheckStrictly(value) {
            this.checkStrictly = !value;
        },
        //根据id获取菜单
        getMenuById(id, menus) {
            var menu;
            for (var i = 0; i < menus.length; i++) {
                if (menus[i].key == id) {
                    menu = menus[i];
                    break;
                } else {
                    if (menus[i].children && menus[i].children) {
                        menu = this.getMenuById(id, menus[i].children);
                        if (menu) break;
                    }
                }
            }
            return menu;
        }
    }
}
</script>

<style scoped>

</style>
